﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace VirtualTournaments.Frontend.MenuControls.TournamentSubscriptionMenu.InnerControls
{
    public partial class TournamentSubscriptionMainMenu : System.Web.UI.UserControl
    {
        public EventHandler GoBackButtonClicked;
        public EventHandler GoToTournamentInfo;
        
        protected void Page_Load(object sender, EventArgs e)
        {
            Services.Users.User user = Session["CurrentUser"] as Services.Users.User;
            //TODO: what if null?
            if (user != null)
            {
                //TODO: should get active tournaments based on logged user!!!
                //Services.Tournaments.Tournament[] activeTournaments = Services.Tournaments.TournamentsService.GetActiveTournaments();
                Services.Tournaments.Tournament[] activeTournaments = GetActiveTournaments(user);

                if (activeTournaments.Length > 0)
                {
                    TableRow newRow;
                    TableCell newCell1, newCell2, newCell3, newCell4;
                    Button sButton;
                    int i = 0;
                    foreach (Services.Tournaments.Tournament tournament in activeTournaments)
                    {
                        newRow = new TableRow();
                        newCell1 = new TableCell();
                        newCell2 = new TableCell();
                        newCell3 = new TableCell();
                        newCell4 = new TableCell();

                        newCell1.Controls.Add(new Label { ID = "Tournament " + i, Text = tournament.Name });
                        newCell1.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

                        newCell2.Controls.Add(new CheckBox { Enabled = false });
                        newCell2.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

                        sButton = new Button { ID = (i + 1).ToString() }; sButton.Text = "Click To Subscribe"; 
                        sButton.Click += new EventHandler(TournamentSubscriptionMainMenu_OnSubscribe);
                        newCell3.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                        newCell3.Controls.Add(sButton);

                        Button infoButton = new Button { ID = "InfoButton" + i, Text = "View Info", BackColor = System.Drawing.Color.Transparent, BorderStyle = System.Web.UI.WebControls.BorderStyle.None };
                        infoButton.Click += new EventHandler(InfoButton_OnClick);
                        newCell4.Controls.Add(infoButton);
                        newCell4.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;

                        newRow.Cells.Add(newCell1);
                        newRow.Cells.Add(newCell2);
                        newRow.Cells.Add(newCell3);
                        newRow.Cells.Add(newCell4);

                        TournamentsTable.Rows.Add(newRow);
                        i++;
                    }

                    CheckToSubscribeLabel.Text = "Check to subscribe";
                }
                else
                    CheckToSubscribeLabel.Text = "There are no active tournaments";

            }
        }

        protected void GoBackButton_OnClick(object sender, EventArgs e)
        {
            //TODO: find a better way to do this
            GoBackButtonClicked(this, new VirtualTournamentsFrontendEvent { SenderMenu = "TournamentSubscriptionMainMenu" });
        }
        
        protected void InfoButton_OnClick(object sender, EventArgs e)
        {
            Button clickedButton = sender as Button;
            //TODO: what if null?
            if (clickedButton != null)
            {
                int rowIndex = Int32.Parse(clickedButton.ID.Replace("InfoButton", ""));
                rowIndex++;
                string tournamentName = (TournamentsTable.Rows[rowIndex].Cells[0].Controls[0] as Label).Text;

                GoToTournamentInfo(this, new VirtualTournamentsFrontendEvent { TournamentName = tournamentName });
            }

        }

        protected Services.Tournaments.Tournament[] GetActiveTournaments(Services.Users.User user) 
        {
            Services.Tournaments.Tournament tournament;
            List<Services.Tournaments.Tournament> tournaments = new List<Services.Tournaments.Tournament>();

            for (int i = 0; i < 10; i++)
            {
                tournament = new Services.Tournaments.Tournament();
                tournament.IsActive = true;
                tournament.Name = "Tournament " + i;

                tournaments.Add(tournament);
            }

            return tournaments.ToArray();
        }

        protected void TournamentSubscriptionMainMenu_OnSubscribe(object sender, EventArgs e) 
        {
            int tournamentIndex = Convert.ToInt32((sender as Button).ID);
            string tournamentName = (TournamentsTable.Rows[tournamentIndex].Cells[0].Controls[0] as Label).Text;

            BetInfoLabel.Text = "Are you sure you want to subscribe to tournament " + tournamentName + "?";
            SubscribeToTournamentIndexLabel.Text = tournamentIndex.ToString();

            pagedimmer.Visible = true;
            msgbox.Visible = true;

        }

        protected void FinishCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            FinishCurrentBetButton.Enabled = false;
            ConfirmCurrentBetButton.Enabled = true;
            CancelCurrentBetButton.Enabled = true;
            StatusLabel.Text = "Waiting for confirmation...";

            pagedimmer.Visible = false;
            msgbox.Visible = false;

        }

        protected void ConfirmCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: process subscription!
            int subscribeToTournamentIndex = Convert.ToInt32(SubscribeToTournamentIndexLabel.Text);
            ((CheckBox)TournamentsTable.Rows[subscribeToTournamentIndex].Cells[1].Controls[0]).Checked = true;

            StatusLabel.Text = "Your bet has been registered! Click 'Finish' to complete the operation.";
            FinishCurrentBetButton.Enabled = true;
            ConfirmCurrentBetButton.Enabled = false;
            CancelCurrentBetButton.Enabled = false;
        }

        protected void CancelCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            pagedimmer.Visible = false;
            msgbox.Visible = false;
        }

    }
}